/**
 * 把项目下的空间列表转为 楼栋 - 楼层 - 空间 的树形结构（不包含项目）
 * @param roomList
 * @returns {[]}
 */
export function parseSceneData(roomList) {
  const edificeData = {}
  const floorData = {}
  const roomData = {}
  roomList.map(item => {
    if (!edificeData[item.sceneEdificeId]) {
      edificeData[item.sceneEdificeId] = {
        parentId: item.sceneProjectId,
        label: item.edificeName,
        value: item.sceneEdificeId,
        children: []
      }
    }

    if (!floorData[item.sceneFloorId]) {
      floorData[item.sceneFloorId] = {
        label: item.floorName,
        value: item.sceneFloorId,
        parentId: item.sceneEdificeId,
        children: []
      }
    }

    if (item.id != null && !roomData[item.id]) {
      roomData[item.id] = {
        label: item.roomName,
        value: item.id,
        parentId: item.sceneFloorId
      }
    }
  })
  for (const key in roomData) {
    const item = roomData[key]
    floorData[item.parentId].children.push(item)
  }
  for (const key in floorData) {
    const item = floorData[key]
    edificeData[item.parentId].children.push(item)
  }
  const result = []
  for (const key in edificeData) {
    const item = edificeData[key]
    result.push(item)
  }
  return result
}

/**
 * 把项目下的空间列表转为 楼栋 - 楼层 - 空间 的树形结构（包含项目）
 * @param roomList
 * @returns {[]}
 */
export function parseSceneDataProject(roomList) {
  const projectData = {}
  const edificeData = {}
  const floorData = {}
  const roomData = {}
  roomList.map(item => {
    if (!projectData[item.sceneProjectId]) {
      if (!item.sceneProjectId) return
      projectData[item.sceneProjectId] = {
        parentId: item.sceneProjectId,
        label: item.projectName,
        type: 'project',
        value: item.sceneProjectId,
        children: []
      }
    }
    if (!edificeData[item.sceneEdificeId]) {
      if (!item.sceneEdificeId) return
      edificeData[item.sceneEdificeId] = {
        parentId: item.sceneProjectId,
        label: item.edificeName,
        type: 'edifice',
        value: item.sceneEdificeId,
        children: []
      }
    }

    if (!floorData[item.sceneFloorId]) {
      if (!item.sceneFloorId) return
      floorData[item.sceneFloorId] = {
        label: item.floorName,
        value: item.sceneFloorId,
        type: 'floor',
        parentId: item.sceneEdificeId,
        children: []
      }
    }

    if (!roomData[item.id]) {
      if (!item.id) return
      roomData[item.id] = {
        label: item.roomName,
        value: item.id,
        type: 'room',
        parentId: item.sceneFloorId
      }
    }
  })

  for (const key in roomData) {
    const item = roomData[key]
    floorData[item.parentId].children.push(item)
  }
  for (const key in floorData) {
    const item = floorData[key]
    edificeData[item.parentId].children.push(item)
  }
  for (const key in edificeData) {
    const item = edificeData[key]
    projectData[item.parentId].children.push(item)
  }
  const result = []
  for (const key in projectData) {
    const item = projectData[key]
    result.push(item)
  }
  return result
}

// 把公司处理成树形结构数据
export function convertToTree(flatData) {
  const tree = {}
  const index = {}

  // Create an index for quick access to each node
  flatData.forEach(item => {
    const currentNode = {
      label: item.companyName,
      value: item.companyId,
      type: 'company',
      children: []
    }

    index[item.companyId] = currentNode

    // If the item has a parent project, add it as a child to the project
    if (item.sceneProjectId) {
      const projectId = item.sceneProjectId

      if (!tree[projectId]) {
        tree[projectId] = {
          label: item.projectName,
          value: projectId,
          type: 'project',
          children: []
        }
      }

      tree[projectId].children.push(currentNode)
    }
  })

  // Convert the tree object to an array
  const result = Object.values(tree)
  const mergedResult = result.map(project => {
    if (project.children.length > 1) {
      // Merge children with the same projectId
      const mergedChildren = project.children.reduce((acc, child) => {
        const existingChild = acc.find(c => c.value === child.value)
        if (existingChild) {
          existingChild.children.push(...child.children)
        } else {
          acc.push({ ...child })
        }
        return acc
      }, [])

      // Remove duplicates from children
      project.children = mergedChildren.filter((child, index, self) => {
        return index === self.findIndex(c => c.value === child.value)
      })
    }
    return project
  })

  return mergedResult
  // return result
}

/**
 * 把空间面积组装为obj
 * @param roomList
 */
export function parseAareToObj(roomList) {
  const result = {}
  roomList.map(item => {
    result[item.id] = item.roomArea
  })
  return result
}

/**
 * 解析成回显的场景数据 - 任一层级单选的场景
 * @param data
 */
export function parseToSceneInfo(data) {
  const sceneInfo = []
  if (data.sceneEdificeId) {
    sceneInfo.push(data.sceneEdificeId)
  }
  if (data.sceneFloorId) {
    sceneInfo.push(data.sceneFloorId)
  }
  if (data.sceneRoomId) {
    sceneInfo.push(data.sceneRoomId)
  }
  return sceneInfo
}

/**
 * 解析成回显的场景数据 - 任一层级单选的场景
 * @param data
 */
export function sceneInfoToParam(sceneInfo) {
  const params = {}
  if (sceneInfo) {
    if (sceneInfo[0]) {
      params.sceneEdificeId = sceneInfo[0]
    }
    if (sceneInfo[1]) {
      params.sceneFloorId = sceneInfo[1]
    }
    if (sceneInfo[2]) {
      params.sceneRoomId = sceneInfo[2]
    }
  }
  return params
}

/**
 * 解析成回显的场景数据 - 多选的场景
 * @param roomList
 */
export function parseToSceneInfos(roomList) {
  const result = []
  roomList.map(item => {
    result.push([item.sceneEdificeId, item.sceneFloorId, item.sceneRoomId])
  })
  return result
}
